import { Define, EntityObjectFactory } from 'tiny-entity2';
const EntityObjectType = EntityObjectFactory.GetEntityObjectType(process.env.dataBaseType || 'mysql');
import { ProductCategory } from './productCategory';
import { Unit } from './unit';
import { Modifier } from './modifier';
import { Combo } from './combo';
import { ImageStore } from './imageStore';
import { StoreStock } from './storeStock';

@Define.Table({ TableName: 'product' })
export class Product extends EntityObjectType<Product>{
    @Define.PrimaryKey()
    id: string;
    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    name: string;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    createTime: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    version: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    code: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    pinyin: string;

    @Define.Column()
    fullPinyin: string;

    @Define.Column()
    numPy: string;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DataLength: 11,
        DecimalPoint: 3,
        NotAllowNULL: true
    })
    price: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        DefaultValue: 'normal'
    })
    status: string;

    @Define.Column({
        DataType: Define.DataType.BOOL
    })
    autoAdd: boolean;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    description: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true,
        NotAllowNULL: true
    })
    storeId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    unitId: string;

    @Define.Mapping({
        Mapping: 'Unit',
        MappingKey: { FKey: "id", MKey: "unitId" },
        MappingType: Define.MappingType.One
    })
    unit: Unit;

    @Define.Column({
        DataType: Define.DataType.INT,
        DefaultValue: 0
    })
    indexNo: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        DefaultValue: 'normal'
    })
    type: string;

    get ProductType() {
        return ProductType.Prase(this.type);
    }

    @Define.Mapping({
        Mapping: 'Combo',
        MappingKey: 'productId',
        MappingType: Define.MappingType.One
    })
    combo: Combo;

    @Define.Mapping({
        Mapping: 'Modifier',
        MappingKey: 'productId'
    })
    modifiers: Modifier[];

    @Define.Mapping({
        Mapping: 'ProductCategory',
        MappingKey: 'productId'
    })
    productCategory: ProductCategory[];

    @Define.Mapping({
        Mapping: 'StoreStock',
        MappingKey: { FKey: 'productId', MKey: 'id' },
        MappingType: Define.MappingType.One
    })
    storeStock: StoreStock;

    @Define.Mapping({
        Mapping: 'ImageStore',
        MappingKey: { FKey: 'objectId', MKey: 'id' }
    })
    images: ImageStore[];
}

export const ProduceStatus = {
    Normal: 'normal',
    Disable: 'disable',
    Delete: 'delete'
}

export class ProductType {
    constructor(
        private name: string,
        private value
    ) { }

    static Prase(value) {
        switch (value) {
            case 'normal': return ProductType.Normal;
            case 'combo': return ProductType.Combo;
            default: throw new Error('不被允许的类型！');
        }
    }

    get Value() {
        return this.value;
    }
    get Name() {
        return this.name;
    }

    static Normal = new ProductType('普通商品', 'normal');
    static Combo = new ProductType('套餐', 'combo');
}